package com.regex.web.baidu.ai.image;

import java.net.URLEncoder;

import com.regex.web.baidu.ai.AuthService;
import com.regex.web.baidu.ai.Base64Util;
import com.regex.web.baidu.ai.FileUtil;
import com.regex.web.baidu.ai.HttpUtil;
import com.regex.web.baidu.ai.NetImageUtils;

/**
 * 身份证识别
 * @author ocean.regex
 *
 */
public class Idcard {
	public static void main(String[] args) {
        // 身份证识别url
        String idcardIdentificate = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
        // 本地图片路径
        String filePath = "C:/Users/admin/Desktop/pic/idCard1.jpg";
        try {
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            // 识别身份证正面id_card_side=front;识别身份证背面id_card_side=back;
            String params = "id_card_side=back&" + URLEncoder.encode("image", "UTF-8") + "="
                    + URLEncoder.encode(imgStr, "UTF-8");
            /**
             * 线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
             */
            String accessToken = AuthService.getAuth();
            String result = HttpUtil.post(idcardIdentificate, accessToken, params);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
	
	/**
	 * 本地图片获取身份证
	 * @param filePath
	 * @param flag true 正面， false 反面 
	 * @return
	 */
	public static String localPicIdCard(String filePath, boolean flag) {
		String result = "";
		// 身份证识别url
        String idcardIdentificate = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
        try {
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            // 识别身份证正面id_card_side=front;识别身份证背面id_card_side=back;
            String params = "";
            if(flag) {
            	params = "id_card_side=front&" + URLEncoder.encode("image", "UTF-8") + "="
                        + URLEncoder.encode(imgStr, "UTF-8");
            } else {
            	params = "id_card_side=back&" + URLEncoder.encode("image", "UTF-8") + "="
            			+ URLEncoder.encode(imgStr, "UTF-8");
            }
            /**
             * 线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
             */
            String accessToken = AuthService.getAuth();
            result = HttpUtil.post(idcardIdentificate, accessToken, params);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
	}
	
	/**
	 * 网络图片身份证识别
	 * @param netPicPath
	 * @param flag
	 * @return
	 */
	public static String netPicIdCard(String netPicPath, boolean flag) {
		String result = "";
		// 身份证识别url
        String idcardIdentificate = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
        try {
            byte[] imgData = NetImageUtils.getImgerFormNetByUrl(netPicPath);
            String imgStr = Base64Util.encode(imgData);
            // 识别身份证正面id_card_side=front;识别身份证背面id_card_side=back;
            String params = "";
            if(flag) {
            	params = "id_card_side=front&" + URLEncoder.encode("image", "UTF-8") + "="
                        + URLEncoder.encode(imgStr, "UTF-8");
            } else {
            	params = "id_card_side=back&" + URLEncoder.encode("image", "UTF-8") + "="
            			+ URLEncoder.encode(imgStr, "UTF-8");
            }
            /**
             * 线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
             */
            String accessToken = AuthService.getAuth();
            result = HttpUtil.post(idcardIdentificate, accessToken, params);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
	}

}
